package com.openxt.nbzj.adapter;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import com.openxt.nbzj.fragment.OpenxtFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 颖杰_2 on 2016/5/20.
 * 更新viewPage Change the FragmentPagerAdapter to FragmentStatePagerAdapter.
 * <p/>
 * Override getItemPosition() method and return POSITION_NONE.
 * <p/>
 * Eventually, it will listen to the notifyDataSetChanged() on view pager.
 */
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    public List<OpenxtFragment> mListViews = new ArrayList<>();
    public Context context;
    private OpenxtFragment currentFragment;

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public void setList(List<OpenxtFragment> mListViews) {
        this.mListViews = mListViews;
    }

    @Override
    public OpenxtFragment getItem(int position) {
        return mListViews.get(position);
    }

    @Override
    public int getCount() {
        return mListViews.size();
    }

    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        if (getCurrentFragment() != object) {
            currentFragment = ((OpenxtFragment) object);
        }
        super.setPrimaryItem(container, position, object);
    }

    /**
     * Get the current fragment
     */
    public OpenxtFragment getCurrentFragment() {
        return currentFragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mListViews.get(position).getName();
    }
}
